<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:level:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="levelList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="等级名称" align="center" prop="godName" />
      <el-table-column label="等级图标" align="center" prop="godIcon">
        <template slot-scope="scope">
          <el-image v-if="scope.row.godIcon" style="width: 50px; height: 50px" 
          :src="scope.row.godIcon" :preview-src-list="[scope.row.godIcon]" />
        </template>
      </el-table-column>
      <el-table-column label="接单数量" align="center" prop="orders" >
        <template slot-scope="scope">
          {{scope.row.orders === -1?'无要求':scope.row.orders === 0?'官方审核':scope.row.orders+'单'}}
        </template>
      </el-table-column>
      <el-table-column label="好评率" align="center" prop="positive" >
        <template slot-scope="scope">
          {{scope.row.positive === -1?'无要求':scope.row.positive === 0?'官方审核':'大于等于'+scope.row.positive+'%'}}
        </template>
      </el-table-column>
      <el-table-column label="最后编辑时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:level:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:level:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大神等级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="等级名称" prop="godName">
          <el-input v-model="form.godName" placeholder="请输入等级名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="等级图标" prop="godIcon" required>
           <SingleUpload :avatar="form.godIcon" @emitSetUrl="setUrl" />
        </el-form-item>
        <el-form-item label="接单要求" prop="ordersClaim">
          <el-radio-group v-model="form.ordersClaim">
            <el-radio :label="-1">无要求</el-radio>
            <el-radio :label="1">数量要求</el-radio>
            <el-radio :label="0">系统审核</el-radio>
          </el-radio-group>
        </el-form-item>
         <el-form-item v-if="form.ordersClaim === 1" label="接单数量" prop="orders">
          <el-input-number v-model="form.orders" placeholder="接单数量" controls-position=right>
          </el-input-number>
        </el-form-item>
        <el-form-item label="好评要求" prop="positiveClaim">
          <el-radio-group v-model="form.positiveClaim">
            <el-radio :label="-1">无要求</el-radio>
            <el-radio :label="1">数量要求</el-radio>
            <el-radio :label="0">系统审核</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.positiveClaim === 1" label="好评率" prop="positive">
          <el-input-number v-model="form.positive" placeholder="%" controls-position=right>
          </el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLevel, getLevel, delLevel, addLevel, updateLevel, exportLevel } from "@/api/system/level";
import SingleUpload from '@/components/Upload/SingleUpload'
export default {
  name: "Level",
  components:{
    SingleUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 大神等级表格数据
      levelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        godIcon: undefined,
        orders: undefined,
        positive: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        godName: [
          { required: true, message: "等级名称不能为空", trigger: "blur" }
        ],
        godIcon: [
          { required: true, message: "等级图标不能为空", trigger: "blur" }
        ],
        ordersClaim: [
          { required: true, message: "选择接单要求", trigger: "blur" }
        ],
        orders: [
          { required: true, message: "接单数量不能为空", trigger: "blur" }
        ],
        positiveClaim: [
          { required: true, message: "选择好评率要求", trigger: "blur" }
        ],
        positive: [
          { required: true, message: "好评率不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    setUrl(data){
      this.form.godIcon = data.url
    },
    /** 查询大神等级列表 */
    getList() {
      this.loading = true;
      listLevel(this.queryParams).then(response => {
        this.levelList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        godName: undefined,
        godIcon: undefined,
        orders: undefined,
        positive: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大神等级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLevel(id).then(response => {
        this.form = response.data;
        this.$set(this.form, 'positiveClaim', response.data.positive<1?response.data.positive:1)
        this.$set(this.form, 'ordersClaim', response.data.orders<1?response.data.orders:1)
        this.open = true;
        this.title = "修改大神等级";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      
      if(this.form.ordersClaim !== 1){
        this.form.orders = this.form.ordersClaim
      }
      if(this.form.positiveClaim !== 1){
        this.form.positive = this.form.positiveClaim
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateLevel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addLevel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除大神等级编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLevel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有大神等级数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportLevel(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    godIconBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 5
      if (!isRightSize) {
        this.$message.error('文件大小超过 5MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
  }
};
</script>